package com.zelic.znn.core;

import java.util.*;

/**
 * @since 17/07/2011
 * @author zelic
 * Filename: Util.java
 * Description: utility for small tasks
 */

/**
 * Util is the collection of simple array tasks
 * @author zelic
 */
public class Util {

    /**
     * Create a random array
     * @param number : number of elements
     * @return random array
     */
    public static double[] randomArray(int number) {
        double[] result = new double[number];

        for (int i = 0; i < number; i++) {
            result[i] = (Math.random()-0.5)/2;
        }
        return result;
    }

    /**
     * Create zero array
     * @param number : number of elements
     * @return zero array
     */
    public static double[] zeroArray(int number) {
        double[] result = new double[number];

        for (int i = 0; i < number; i++) {
            result[i] = 0;
        }
        return result;
    }

    /**
     * Create random matrix
     * @param height: height of the matrix
     * @param width: width of the matrix
     * @return random matrix
     */
    public static double[][] randomMatrix(int height, int width) {
        double[][] result = new double[height][width];

        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                result[i][j] = Math.sin(Math.random()*2*3.14);
                
                
            }
        }
        return result;
    }
    
    /**
     * Create random matrix
     * @param height: height of the matrix
     * @param width: width of the matrix
     * @return random matrix
     */
    public static double[][] NguyenWidrowRandomMatrix(int height, int width) {
        double[][] result = new double[height][width];
        double beta = 0.7*Math.pow((double)height, 1/(double)width);
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                result[i][j] = Math.sin(Math.random()*2*3.14)*beta;              
            }
        }
        return result;
    }
    
    
    /**
     * Create a zero matrix
     * @param height
     * @param width
     * @return zero matrix
     */
    public static double[][] zeroMatrix(int height, int width) {
        double[][] result = new double[height][width];

        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                result[i][j] = 0;
            }
        }
        return result;
    }
    
    
    /**
     * Create an array with bias
     * @param input array
     * @return biasArray
     */
    public static double[] biasArray(double[] input){
        int length = input.length+1;
        double[] result = new double[length];
        result[0]=1;
        for (int i=1;i<length;i++){
            result[i]=input[i-1];
        }
        return result;
    }
    
    /**
     * Extract a specific row of a matrix
     * @param row
     * @param elements
     * @param matrix
     * @return 
     */
    public static double[] extractMatrixRow(int row, int elements, double[][] matrix){
        double[] result = new double[elements];
        for (int i=0;i<elements;i++){
            result[i] = matrix[row][i];
        }
        return result;
    }
    
    /**
     * 
     * @param vector 
     */
    public static void showVector(double[] vector, String name){
        System.out.print(name+": \n");
        for (int i=0;i<vector.length;i++){
            System.out.printf(" "+vector[i]);
        }
        System.out.printf("\n------------------------\n");
    }
    
    /**
     * 
     * @param height
     * @param width
     * @param matrix 
     */
    public static void showMatrix(int height, int width, double[][] matrix, String name){
        System.out.print(name+": \n");
        for (int i=0;i<height;i++){
            for (int j=0; j<width;j++){
                System.out.printf(" "+matrix[i][j]);
            }
            System.out.printf("\n");
        }
        System.out.printf("\n------------------------\n");
    }
    
    public static double[] winnerArray(double[] input){
        double[] result = new double[input.length];
        double max = input[0];
        int index=0;
        for (int i=0;i<input.length;i++){
            if (input[i]>max){
                index=i;
                max = input[i];
            }
        }
        for (int i=0;i<input.length;i++){
            result[i]=0;
        }
        result[index]=1;
        return result;
    }
    
    public static double[] estimateDoubleArray(double[] input){
        double[] result = new double[input.length];
        for (int i=0;i<input.length;i++){
            if (input[i]>0){
                result[i]=1;
            } else {
                result[i]=-1;
            }
        }
        return result;
    }
}
